﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WinAppStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter(@"E:\winApp.log",true);
            Console.SetOut(sw);            
            Console.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now));
            Console.WriteLine("\n输入的参数：");
            Console.WriteLine("args[0]:"+args[0]);
            String fullFileName = args[0];
            if (fullFileName.Contains("?"))
            {
                fullFileName = fullFileName.Split('?')[1];
            }
            Console.WriteLine("UrlDecode转换前:" + fullFileName);
            
            fullFileName=HttpUtility.UrlDecode(fullFileName,Encoding.UTF8);
           
            Console.WriteLine("UrlDecode转换后:" + fullFileName);
            Boolean isStart = true;
            Console.WriteLine("是否启动应用程序:" + isStart);
            String cmdArgs = "";
            String fileName = fullFileName;
            if (fullFileName.Contains("#"))
            {
                fileName = fullFileName.Split('#')[0];
                cmdArgs= fullFileName.Split('#')[1];
            }
            Console.WriteLine("启动的应用程序完整路径:" + fileName);
            Console.WriteLine("启动应用程序的参数:"+cmdArgs);
            if (isStart) startApp(fileName, cmdArgs);
            sw.Flush();
            sw.Close();
            Console.ReadKey();
             
        }

        static void startApp(String fileName,String args)
        {
            System.Diagnostics.Process appProcess = new System.Diagnostics.Process();
            appProcess.StartInfo.FileName = fileName; //"D:\\百度下载\\PanDownload\\PanDownload.exe";            
            appProcess.StartInfo.Arguments= args;
            //appProcess.StartInfo.Arguments = @"http://www.baidu.com ";
            appProcess.Start();
        }       
    }
}
